package com.bosicen.crm.web.controller.sysmanage;

import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bosicen.crm.dao.entity.Company;
import com.bosicen.crm.dao.entity.ProductType;
import com.bosicen.crm.dao.entity.User;
import com.bosicen.crm.service.sysmanage.ProductTypeServiceImpl;
import com.bosicen.crm.tools.AppUtil;
import com.bosicen.crm.tools.Page;
import com.google.gson.Gson;

/***
 * 商品分类
 * 
 * @author tigerlei
 * 
 */
@Controller
@RequestMapping("/basicinfo")
public class ProductTypeController {

	@Autowired
	public ProductTypeServiceImpl productTypeServiceImpl;

	/****
	 * 查询产品类型列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/findProductType")
	public String findProductType(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("userinfo");
		Page page = new Page();
		page.setEveryPage(10);
		try {
			page.setCurrentPage(AppUtil.ObjectToInt(request.getParameter("pageno")));
			page = this.productTypeServiceImpl.findProductType4Page(user.getCompanyid(), page);
			request.setAttribute("page", page);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return "sysmanage/ProductType_list";
	}

	/****
	 * 保存新增/修改 产品类型信息
	 * 
	 * @param ps
	 * @param request
	 */
	@RequestMapping(value = "/cudProductType/{type}")
	public void cudProductType(ProductType ps, HttpServletRequest request, @PathVariable("type") Integer type, Writer writer) {
		Gson gson = new Gson();
		boolean flag = false;
		try {
			User user = (User) request.getSession().getAttribute("userinfo");
			ps.setCompanyid(user.getCompanyid());
			switch (type) {
			case 0:
				flag = productTypeServiceImpl.delProductType(ps);
				break;
			case 1:
				flag = productTypeServiceImpl.saveOrUpdateProductType(ps);
				break;
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				writer.write(gson.toJson(flag));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 新增,修改信息
	 * 
	 * @param company
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addProductType/{type}")
	public String addProductType(HttpServletRequest request, @PathVariable("type") Integer type) {
		String id = request.getParameter("id");
		ProductType productType = null;
		if (type != 0 && StringUtils.isNotBlank(id)) {
			productType = productTypeServiceImpl.findProductTypeById(AppUtil.ObjectToInt(id));
		}
		request.setAttribute("productType", productType);
		return "sysmanage/addProductType";
	}

	/****
	 * 查询产品类型列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/findProductTypelist/{type}")
	public void findProductTypelist(HttpServletRequest request, @PathVariable("type") Integer type, Writer writer) {
		User user = (User) request.getSession().getAttribute("userinfo");
		Gson gson = new Gson();
		ProductType ptype = new ProductType();
		List<ProductType> list = null;
		ptype.setCompanyid(user.getCompanyid());
		ptype.setFlag(type);
		try {
			 list = productTypeServiceImpl.findProductTypeList(ptype);
		} catch (Exception ex) {
			ex.printStackTrace();
		}finally {
			try {
				writer.write(gson.toJson(list));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}
